# -*- coding: utf-8 -*-
#HSI颜色空间中的分割
from skimage import data
import matplotlib.pyplot as plt
import numpy as np

from rgb2hsi import rgb2hsi


#HSI空间分割
def main(image):
    hsi_image=np.zeros(image.shape,dtype='uint8')#创建相同维度
    
    for ii in range(image.shape[0]):
        for jj in range(image.shape[1]):
            r,g,b=image[ii,jj,:]
            h,s,i=rgb2hsi(r,g,b)#调用rgb转hsi函数
            hsi_image[ii,jj,:]=(h,s,i)
    H=hsi_image[:,:,0]#获取各个通道
    S=hsi_image[:,:,1]
    I=hsi_image[:,:,2]
    
    
    #生成二值饱和度模板
    S_template=np.zeros(S.shape,dtype='uint8')
    for i in range(S.shape[0]):
        for j in range(S.shape[1]):
            if S[i,j]>0.3*S.max():
                S_template[i,j]=1
    
    #色调图像与而值饱和度模板相乘可得分割结果F
    F=np.zeros(H.shape,dtype='uint8')
    for i in range(F.shape[0]):
        for j in range(F.shape[1]):
            F[i,j]=H[i,j]*S_template[i,j]
    return image,H,S,I,S_template,F

#显示结果
#image=data.coffee()
#image1=main(image)
#
#plt.figure()
#plt.axis('off')
#plt.imshow(image1[0])#显示原始图像
#plt.figure()
#plt.axis('off')
#plt.imshow(image1[1],cmap='gray')#显示H分量
#plt.figure()
#plt.axis('off')
#plt.imshow(image1[2],cmap='gray')#显示S分量
#plt.figure()
#plt.axis('off')
#plt.imshow(image1[3],cmap='gray')#显示I分量
#plt.figure()
#plt.axis('off')
#plt.imshow(image1[4],cmap='gray')#显示二值饱和度模板
#plt.figure()
#plt.axis('off')
#plt.imshow(image1[5],cmap='gray')
#            